package com.yune.activity.account;

import android.app.AlertDialog;
import android.app.Application;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.lifecycle.AndroidViewModel;

import com.yune.R;
import com.yune.activity.App;
import com.yune.activity.login.LoginActivity;
import com.yune.databinding.ActivityAccountBinding;
import com.yune.utils.Network;

import org.json.JSONException;
import org.json.JSONObject;

public class AccountViewModel extends AndroidViewModel {
    private String [] data = {"我的账号:","我的邮箱:","我的昵称:","修改密码"};
    public AccountViewModel(@NonNull Application application) {
        super(application);
    }

    public void setBinding(ActivityAccountBinding binding, AccountActivity accountActivity) {
        binding.setAdp(new ArrayAdapter<>(accountActivity, android.R.layout.simple_list_item_1, data));
        //setOnItemClickListener注册一个回调，以便在单击此AdapterView中的项时调用。ItemClickListener项目单击监听
        binding.accountList.setOnItemClickListener((parent, view, position, id) -> {//position位置
           if (position==2){
               EditText editText=new EditText(accountActivity);
               editText.setHint("请设置昵称");
               //对话框
               AlertDialog.Builder dialog=new AlertDialog.Builder(accountActivity);
               dialog.setTitle("修改昵称").setView(editText).setPositiveButton("确定", (dialog1, which) -> {//Positive确定
                   String str=editText.getText().toString() ;
                   if (!str.equals("")){//equals等于
                       JSONObject jo=new JSONObject();
                       try {
                           jo.put("nickname",str);
                       } catch (JSONException e) {//JSONException异常
                           e.printStackTrace();//printStackTrace  print打印，Stack堆叠，Trace痕迹
                       }
                       /**
                        * 参数：
                        * key–要检索的首选项的名称。
                        * defValue–如果此首选项不存在，则返回的值。
                        */
                       String loginMsg = App.sp.getString("loginMsg", "");
                       try {
                           JSONObject jsonObject=new JSONObject(loginMsg);//一组可修改的名称/值映射。名称是唯一的非空字符串。
                           Network.put(jo, "https://api2.bmob.cn/1/users/"+ jsonObject.getString("objectId"),null);
                       } catch (JSONException e) {
                           e.printStackTrace();
                       }
                       data[position]="我的昵称:"+str;
                       Toast.makeText(accountActivity, "昵称已更新", Toast.LENGTH_SHORT).show();
                       //refresh刷新
                       App.refreshLoginMsg(App.sp.getString("account",""),App.sp.getString("password",""));
                   }else {
                       Toast.makeText(accountActivity, "请输入昵称", Toast.LENGTH_SHORT).show();
                   }
               }).setNegativeButton("取消",null).show();
           }     //NegativeButton否定按钮
           if (position==3){
               AlertDialog.Builder dia=new AlertDialog.Builder(accountActivity);
               dia.setTitle("修改昵称").setMessage("是否发送改密邮件").setPositiveButton("确定", (dialog, which) -> {
                   EditText email=new EditText(accountActivity);
                   email.setHint("请输入邮件");
                   AlertDialog.Builder dia1=new AlertDialog.Builder(accountActivity);
                   dia1.setTitle("改密").setView(email).setPositiveButton("确定", (dialog1, which1) -> {
                       JSONObject jo=new JSONObject();
                       try {
                           jo.put("email",email.getText().toString().trim());//trim修剪
                           Network.post(jo, "https://api2.bmob.cn/1/requestPasswordReset", res ->{//requestPasswordReset请求密码重置
                               accountActivity.runOnUiThread(() -> Toast.makeText(accountActivity,"已发送改密邮件",Toast.LENGTH_SHORT).show());
                           });               //  runOnUiThread运行超线程
                       } catch (JSONException e) {
                           e.printStackTrace();
                       }
                   }).setNegativeButton("取消",null).show();
               }).setNegativeButton("取消",null).show();
           }
        });
        String loginMsg = App.sp.getString("loginMsg", "");
        try {
            //使用JSON字符串中的名称/值映射创建新的JSONObject。
            JSONObject jsonObject=new JSONObject(loginMsg);
            data[0]="我的账号:"+jsonObject.getString("username");
            data[1]="我的邮箱:"+jsonObject.getString("email");
            data[2]="我的昵称:"+jsonObject.getString("nickname");
        } catch (JSONException ignored) {//ignored忽略
        }
        binding.getAdp().notifyDataSetChanged();//notifyDataSetChanged通知数据设置改变
    }
    /**notifyDataSetChanged
     * 通知附加的观察者基础数据已更改，任何反映数据集的视图都应刷新自身。
     * 覆盖：
     * 类BaseAdapter中的notifyDataSetChanged
     */
}
